<?php
/**
 * Created by PhpStorm.
 * User: 孙家浩
 * Date: 2019/4/25
 * Time: 17:16
 */

namespace app\home;

use think\Container;
use think\Controller;
use think\facade\Request;
use think\facade\View;

class Base extends Controller
{
	private $whiteList = [
		'login',
		'register',
	];

	protected $currentUser;

	public function initialize()
	{
		if (!in_array(Request::pathinfo(), $this->whiteList)) {
			$this->currentUser = session('currentUser');
			if (empty($this->currentUser)) {
				$this->currentUser = cookie('currentUser');
				if (empty($this->currentUser)) {
					$this->redirect('login');
				}
				$this->currentUser = json_decode($this->currentUser,true);
			}
		}
		View::assign('currentUser',$this->currentUser);
	}

	/**
	 * 操作成功跳转的快捷方法
	 * @access public
	 * @param mixed $msg 提示信息
	 * @param string $url 跳转的URL地址
	 * @param mixed $data 返回的数据
	 * @return void
	 * 当 msg为数组,则 需遵守配置规则
     * title - 标题
     * msg - 内容
	 * icon - 图标 0-6
	 * anim - 弹出动画 0-6
	 * time - 自动关闭所需毫秒
     * offset: 'auto'	默认坐标，即垂直水平居中
	 * offset: '100px'	只定义top坐标，水平保持居中
	 * offset: ['100px', '50px']	同时定义top、left坐标
	 * offset: 't'	快捷设置顶部坐标
	 * offset: 'r'	快捷设置右边缘坐标
	 * offset: 'b'	快捷设置底部坐标
	 * offset: 'l'	快捷设置左边缘坐标
	 * offset: 'lt'	快捷设置左上角
	 * offset: 'lb'	快捷设置左下角
	 * offset: 'rt'	快捷设置右上角
	 * offset: 'rb'	快捷设置右下角
	 */
	public function info($msg = '', string $url = null, $data = '')
	{
		if (is_array($msg)) {
			$message = $msg['msg'];
			unset($msg['msg']);
			if (isset($msg['time'])) {
				$msg['time'] = $msg['time']*1000;
			}
			$info = [
				'msg' => $message,
				'config' => $msg,
			];
		} else {
			$info = [
				'msg' => $msg
			];
		}
		cache('info', json_encode($info));
		if ($url) {
			$url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : Container::pull('route')->buildUrl($url);
			$this->redirect($url,$data);
		}
	}

}
